#include "OutputWindow.h"

#include <QtGui>
#include <QDateTime>

OutputWindow::OutputWindow()
{
  name="file.txt";

  box = new QGroupBox("Save results",this);
  box->setCheckable(true);
  copyParams = new QCheckBox("Copy Parameters File");
  copyParams->setToolTip("copy the parameters into a file which will have the same name as the output file"); 
  QGroupBox *nameBox = new QGroupBox("File Name");
  filename = new QLineEdit();
  filename->setFixedWidth(300);
  filename->insert(name);
  prefix = new QCheckBox("prefix");
  date = new QCheckBox("date");
  current = new QCheckBox("Current");
  field = new QCheckBox("Field");
  length = new QCheckBox("Pulse length");
  simType = new QCheckBox("Simulation Type");
  other = new QCheckBox("other");
  other->setEnabled(false);
  prefixText = new QLineEdit(); 
  prefixText->hide();
  QGridLayout *nameLayout = new QGridLayout();
  nameLayout->addWidget(filename,0,0,1,2);
  nameLayout->addWidget(prefix,1,0,1,1);
  nameLayout->addWidget(date,2,0,1,1);
  nameLayout->addWidget(current,3,0,1,1);
  nameLayout->addWidget(field,4,0,1,1);
  nameLayout->addWidget(length,5,0,1,1);
  nameLayout->addWidget(simType,6,0,1,1);
  nameLayout->addWidget(other,7,0,1,1);
  nameLayout->addWidget(prefixText,1,1,1,1);
  nameBox->setLayout(nameLayout);

 
  QVBoxLayout *layout = new QVBoxLayout();
  layout->addWidget(copyParams);
  layout->addWidget(nameBox);
  box->setLayout(layout);

  QObject::connect(date,SIGNAL(clicked()),this,SLOT(setTime()));
  QObject::connect(current,SIGNAL(clicked()),this,SLOT(setCurrent()));
  QObject::connect(field,SIGNAL(clicked()),this,SLOT(setField()));
  QObject::connect(simType,SIGNAL(clicked()),this,SLOT(setSimulType()));
  QObject::connect(length,SIGNAL(clicked()),this,SLOT(setLength()));
  QObject::connect(prefix,SIGNAL(clicked()),this,SLOT(setPrefix()));
  QObject::connect(prefix,SIGNAL(clicked()),this,SLOT(displayPrefix()));
}

OutputWindow::~OutputWindow()
{

}

void OutputWindow::setTime()
{
  updateFileName("date",date);
}

void OutputWindow::setSimulType()
{
  updateFileName("simul",simType);
}

void OutputWindow::setCurrent()
{  
  updateFileName("IxxmA",current);
}

void OutputWindow::setField()
{
  updateFileName("BxxmT",field);
}

void OutputWindow::setLength()
{
  updateFileName("lxxns",length);
}

void OutputWindow::setPrefix()
{

  updateFileName("prefix",prefix);
}

void OutputWindow::updateFileName(QString string,QCheckBox *checkBox)
{
  name = filename->text();
  name.remove("file");
  string+="_";
  if(!name.contains(string) && checkBox->isChecked())
    {
      name.insert(0,string);
    }
  if(name.contains(string) && !(checkBox->isChecked()))
    {
      name.remove(string); 
    }
  name.replace("_.txt",".txt");
  filename->setText(name);
}

QString OutputWindow::getFileName()
{
  QString file = filename->text();
  if(!box->isChecked())
    {
      return "temp.txt";
    }
  else 
    {
      if(filename->text().contains("prefix"))
	{
	  file += prefixText->text();
	}
      return file;
    }
}

void OutputWindow::displayPrefix()
{
  if(prefix->isChecked())
    {
      prefixText->show();
    }
  else
    {
      prefixText->hide();
    }
}

bool OutputWindow::getCopyParams()
{
  if(copyParams->isChecked())
    {
      return true;
    }
  else
    {
      return false;
    }
}
